package tech.aistar.day07.homework;

/**
 * 本类用来演示: 删除字符串
 *
 * @author: success
 * @date: 2022/7/27 8:37 上午
 */
public class StringDelDemo {
    public static void main(String[] args) {
        String oldStr = "hellojavahello";
        System.out.println(delByTarget(oldStr, "hello"));
    }

    public static String delByTarget(String oldStr,String targetStr){
       //return oldStr.replace(targetStr,"");//直接替换,替换所有的
        //参数有效性校验

        //1. 指定递归出口
        if(!oldStr.contains(targetStr)){
            return oldStr;
        }
        //2. java.lang.StringBuilder -> StringBuilder delete(int start,int end);
        // [start,end)
        // 确定targetStr在oldStr中第一次出现的下标的位置
        int startIndex = oldStr.indexOf(targetStr);
        int endIndex = startIndex + targetStr.length();
        //递归调用自己
        return delByTarget(new StringBuilder(oldStr).delete(startIndex,endIndex).toString(),targetStr);
    }
}
